<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>MPI_Allreduce的在OpenMPI、MPICH中的实现 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="该页面整理了MPI_Allreduce在OpenMPI和MPICH中的设计与最佳算法选择。">
<meta property="og:type" content="article">
<meta property="og:title" content="MPI_Allreduce的在OpenMPI、MPICH中的实现">
<meta property="og:url" content="https://chudod.gitee.io/myworks/2022/04/13/OpenMPI&MPICH_Allreduce/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="该页面整理了MPI_Allreduce在OpenMPI和MPICH中的设计与最佳算法选择。">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://note.youdao.com/yws/api/personal/file/WEBef75c5797a8fe9a7d3afe7d9f84db3a2?method=download&shareKey=0412dcb1d5f95516723864a4f1a48a13">
<meta property="og:image" content="https://note.youdao.com/yws/api/personal/file/WEBe38b3f100dd6599ec413faa1ee25edcf?method=download&shareKey=0412dcb1d5f95516723864a4f1a48a13">
<meta property="article:published_time" content="2022-04-12T16:00:00.000Z">
<meta property="article:modified_time" content="2022-05-01T09:13:20.991Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="MPI">
<meta property="article:tag" content="并行计算">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://note.youdao.com/yws/api/personal/file/WEBef75c5797a8fe9a7d3afe7d9f84db3a2?method=download&shareKey=0412dcb1d5f95516723864a4f1a48a13">
  
    <link rel="alternate" href="/myworks/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/myworks/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/myworks/css/style.css">

  
    
<link rel="stylesheet" href="/myworks/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 6.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/myworks/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/myworks/">Home</a>
        
          <a class="main-nav-link" href="/myworks/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/myworks/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://chudod.gitee.io/myworks"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-OpenMPI&amp;MPICH_Allreduce" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/myworks/2022/04/13/OpenMPI&MPICH_Allreduce/" class="article-date">
  <time class="dt-published" datetime="2022-04-12T16:00:00.000Z" itemprop="datePublished">2022-04-13</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/myworks/categories/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/">并行计算</a>►<a class="article-category-link" href="/myworks/categories/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/MPI%E9%9B%86%E5%90%88%E9%80%9A%E4%BF%A1%E7%AE%97%E6%B3%95/">MPI集合通信算法</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      MPI_Allreduce的在OpenMPI、MPICH中的实现
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="Allreduce算法列表"><a href="#Allreduce算法列表" class="headerlink" title="Allreduce算法列表"></a>Allreduce算法列表</h1><h2 id="1、OpenMPI-4-1中的MPI-Allreduce算法"><a href="#1、OpenMPI-4-1中的MPI-Allreduce算法" class="headerlink" title="1、OpenMPI-4.1中的MPI_Allreduce算法"></a>1、OpenMPI-4.1中的MPI_Allreduce算法</h2><p>$\verb+OpenMPI-4.1.2+$是最新版本的$\verb+OpenMPI+$，算法的具体选择在$\verb+ompi&#x2F;mca&#x2F;coll&#x2F;tuned&#x2F;coll_tuned_decision_fixed.c+$和$\verb+ompi&#x2F;mca&#x2F;coll&#x2F;tuned&#x2F;coll_tuned_decision_dynamic.c+$文件里，用户可以指定规则以及选择使用的算法，并且$\verb+OpenMPI+$使用了6种算法，分别是</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Algorithms:</span><br><span class="line">   &#123;<span class="number">1</span>, <span class="string">&quot;basic_linear&quot;</span>&#125;: Reduce + Broadcast</span><br><span class="line">   &#123;<span class="number">2</span>, <span class="string">&quot;nonoverlapping&quot;</span>&#125;: Reduce +Broadcast</span><br><span class="line">   &#123;<span class="number">3</span>, <span class="string">&quot;recursive_doubling&quot;</span>&#125;: Recursive Doubling</span><br><span class="line">   &#123;<span class="number">4</span>, <span class="string">&quot;ring&quot;</span>&#125;: <span class="built_in">Ring</span>(Segmented Messages) + <span class="built_in">Allgather</span>(Ring)</span><br><span class="line">   &#123;<span class="number">5</span>, <span class="string">&quot;segmented_ring&quot;</span>&#125;: Segmented Ring</span><br><span class="line">   &#123;<span class="number">6</span>, <span class="string">&quot;rabenseifner&quot;</span>&#125;: Reduce-Scatter + Allgather</span><br><span class="line">   <span class="comment">/* Currently, ring, segmented ring, and rabenseifner do not support non-commutative operations. */</span></span><br></pre></td></tr></table></figure>
<p>默认使用$\verb+&#x2F;coll_tuned_decision_fixed.c+$里的规则，具体的选择方法如下(原代码是100多行的$else-if$，贼暴力)：<br><img src="https://note.youdao.com/yws/api/personal/file/WEBef75c5797a8fe9a7d3afe7d9f84db3a2?method=download&shareKey=0412dcb1d5f95516723864a4f1a48a13"><br><img src="https://note.youdao.com/yws/api/personal/file/WEBe38b3f100dd6599ec413faa1ee25edcf?method=download&shareKey=0412dcb1d5f95516723864a4f1a48a13"><br>除了默认的规则之外，用户还可以指定参数来选择对应的算法。  </p>
<h2 id="2、MPICH中的MPI-Allreduce算法"><a href="#2、MPICH中的MPI-Allreduce算法" class="headerlink" title="2、MPICH中的MPI_Allreduce算法"></a>2、MPICH中的MPI_Allreduce算法</h2><p>$\verb+MPICH-4.0.2+$是最新版本的$\verb+MPICH+$，在$\verb+MPICH+$中调用$\verb+MPIR_Csel_search+$函数来确定参数，$\verb+MPIR_Csel_search+$返回的值决定采用何种算法。算法的选择逻辑在$\verb+mpich-4.0.2&#x2F;src&#x2F;mpi&#x2F;coll&#x2F;mpir_coll.c+$的4496行的$\verb+MPIR_Allreduce_allcomm_auto+$函数处，$\verb+MPICH+$中的5种算法如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Algorithms:</span><br><span class="line">    <span class="number">1</span>-&gt; <span class="string">&quot;MPIR_Allreduce_intra_smp&quot;</span>: Reduce + Broadcast;</span><br><span class="line">    <span class="number">2</span>-&gt; <span class="string">&quot;MPIR_Allreduce_intra_recursive_doubling&quot;</span>: Recursive Doubling;</span><br><span class="line">    <span class="number">3</span>-&gt; <span class="string">&quot;MPIR_Allreduce_intra_reduce_scatter_allgather&quot;</span>: Scatter + Allgather;</span><br><span class="line">    <span class="number">4</span>-&gt; <span class="string">&quot;MPIR_Allreduce_inter_reduce_exchange_bcast&quot;</span>: a variant of Reduce + Broadcast;</span><br><span class="line">    <span class="number">5</span>-&gt; <span class="string">&quot;MPIR_Allreduce_allcomm_nb&quot;</span>: Nonblocking algorithm, namely MPIR_Iallreduce;</span><br></pre></td></tr></table></figure>

<h1 id="Allreduce算法实现"><a href="#Allreduce算法实现" class="headerlink" title="Allreduce算法实现"></a>Allreduce算法实现</h1><h2 id="1、-Reduce-Broadcast-算法"><a href="#1、-Reduce-Broadcast-算法" class="headerlink" title="1、$Reduce$+$Broadcast$算法"></a>1、$Reduce$+$Broadcast$算法</h2><h3 id="OpenMPI版"><a href="#OpenMPI版" class="headerlink" title="OpenMPI版"></a>OpenMPI版</h3><h4 id="basic-linear函数"><a href="#basic-linear函数" class="headerlink" title="basic_linear函数"></a>basic_linear函数</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">ompi_coll_base_allreduce_intra_basic_linear</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *sbuf, <span class="type">void</span> *rbuf, <span class="type">int</span> count, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="keyword">struct</span> <span class="type">ompi_datatype_t</span> *dtype, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="keyword">struct</span> <span class="type">ompi_op_t</span> *op, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="keyword">struct</span> <span class="type">ompi_communicator_t</span> *comm,</span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="type">mca_coll_base_module_t</span> *<span class="keyword">module</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">/* Reduce to 0 and broadcast. */</span></span><br><span class="line">    <span class="keyword">if</span> (MPI_IN_PLACE == sbuf) &#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="number">0</span> == rank)  </span><br><span class="line">            err = <span class="built_in">ompi_coll_base_reduce_intra_basic_linear</span> (MPI_IN_PLACE, rbuf, count, dtype, op, <span class="number">0</span>, comm, <span class="keyword">module</span>);</span><br><span class="line">        <span class="keyword">else</span> </span><br><span class="line">            err = <span class="built_in">ompi_coll_base_reduce_intra_basic_linear</span>(rbuf, <span class="literal">NULL</span>, ...);</span><br><span class="line">    &#125; </span><br><span class="line">    <span class="keyword">else</span> &#123;</span><br><span class="line">        err = <span class="built_in">ompi_coll_base_reduce_intra_basic_linear</span>(sbuf, rbuf, ...);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (MPI_SUCCESS != err) <span class="keyword">return</span> err;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">ompi_coll_base_bcast_intra_basic_linear</span>(rbuf, count, dtype, <span class="number">0</span>, comm, <span class="keyword">module</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>其中$\verb+MPI_IN_PLACE+$参数用在$\verb+MPI_GATHER+$、$\verb+MPI_Reduce+$等有$\verb+send_buf+$和$\verb+recv_buf+$的函数中，用来代替$\verb+send_buf+$，说明当前进程既发送又接受数据，而且要发送的数据和在要接收的数据的保存在同一内存。</p>
<h4 id="nonoverlapping函数"><a href="#nonoverlapping函数" class="headerlink" title="nonoverlapping函数"></a>nonoverlapping函数</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">ompi_coll_base_allreduce_intra_nonoverlapping</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *sbuf, <span class="type">void</span> *rbuf, <span class="type">int</span> count, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="keyword">struct</span> <span class="type">ompi_datatype_t</span> *dtype, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="keyword">struct</span> <span class="type">ompi_op_t</span> *op, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="keyword">struct</span> <span class="type">ompi_communicator_t</span> *comm,</span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="type">mca_coll_base_module_t</span> *<span class="keyword">module</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    rank = <span class="built_in">ompi_comm_rank</span>(comm);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//第一个参数是sbuf 或者是 MPI_IN_PLACE</span></span><br><span class="line">    comm-&gt;c_coll-&gt;<span class="built_in">coll_reduce</span> (sbuf, rbuf, count, dtype, op, <span class="number">0</span>, comm, comm-&gt;c_coll-&gt;coll_reduce_module);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (MPI_SUCCESS != err) <span class="keyword">return</span> err;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> comm-&gt;c_coll-&gt;<span class="built_in">coll_bcast</span>(rbuf, count, dtype, <span class="number">0</span>, comm, comm-&gt;c_coll-&gt;coll_bcast_module);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="MPICH版"><a href="#MPICH版" class="headerlink" title="MPICH版"></a>MPICH版</h3><h4 id=""><a href="#" class="headerlink" title=""></a></h4><h2 id="2、-Recursive-Doubling-算法"><a href="#2、-Recursive-Doubling-算法" class="headerlink" title="2、$Recursive$ $Doubling$算法"></a>2、$Recursive$ $Doubling$算法</h2><h3 id="OpenMPI版-1"><a href="#OpenMPI版-1" class="headerlink" title="OpenMPI版"></a>OpenMPI版</h3><h4 id="recursivedoubling函数"><a href="#recursivedoubling函数" class="headerlink" title="recursivedoubling函数"></a>recursivedoubling函数</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*         算法保持了规约操作的顺序，所以可支持满足以及不满足交换律的操作</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *         Example on 7 nodes:</span></span><br><span class="line"><span class="comment"> *         Initial state</span></span><br><span class="line"><span class="comment"> *         #      0       1      2       3      4       5      6</span></span><br><span class="line"><span class="comment"> *               [0]     [1]    [2]     [3]    [4]     [5]    [6]</span></span><br><span class="line"><span class="comment"> *         Initial adjustment step for non-power of two nodes.</span></span><br><span class="line"><span class="comment"> *         old rank      1              3              5      6</span></span><br><span class="line"><span class="comment"> *         new rank      0              1              2      3</span></span><br><span class="line"><span class="comment"> *                     [0+1]          [2+3]          [4+5]   [6]</span></span><br><span class="line"><span class="comment"> *         Step 1</span></span><br><span class="line"><span class="comment"> *         old rank      1              3              5      6</span></span><br><span class="line"><span class="comment"> *         new rank      0              1              2      3</span></span><br><span class="line"><span class="comment"> *                     [0+1+]         [0+1+]         [4+5+]  [4+5+]</span></span><br><span class="line"><span class="comment"> *                     [2+3+]         [2+3+]         [6   ]  [6   ]</span></span><br><span class="line"><span class="comment"> *         Step 2</span></span><br><span class="line"><span class="comment"> *         old rank      1              3              5      6</span></span><br><span class="line"><span class="comment"> *         new rank      0              1              2      3</span></span><br><span class="line"><span class="comment"> *                     [0+1+]         [0+1+]         [0+1+]  [0+1+]</span></span><br><span class="line"><span class="comment"> *                     [2+3+]         [2+3+]         [2+3+]  [2+3+]</span></span><br><span class="line"><span class="comment"> *                     [4+5+]         [4+5+]         [4+5+]  [4+5+]</span></span><br><span class="line"><span class="comment"> *                     [6   ]         [6   ]         [6   ]  [6   ]</span></span><br><span class="line"><span class="comment"> *         Final adjustment step for non-power of two nodes</span></span><br><span class="line"><span class="comment"> *         #      0       1      2       3      4       5      6</span></span><br><span class="line"><span class="comment"> *              [0+1+] [0+1+] [0+1+]  [0+1+] [0+1+]  [0+1+] [0+1+]</span></span><br><span class="line"><span class="comment"> *              [2+3+] [2+3+] [2+3+]  [2+3+] [2+3+]  [2+3+] [2+3+]</span></span><br><span class="line"><span class="comment"> *              [4+5+] [4+5+] [4+5+]  [4+5+] [4+5+]  [4+5+] [4+5+]</span></span><br><span class="line"><span class="comment"> *              [6   ] [6   ] [6   ]  [6   ] [6   ]  [6   ] [6   ]</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">ompi_coll_base_allreduce_intra_recursivedoubling</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *sbuf, <span class="type">void</span> *rbuf, <span class="type">int</span> count, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                    <span class="keyword">struct</span> <span class="type">ompi_datatype_t</span> *dtype, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                    <span class="keyword">struct</span> <span class="type">ompi_op_t</span> *op, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                    <span class="keyword">struct</span> <span class="type">ompi_communicator_t</span> *comm,</span></span></span><br><span class="line"><span class="params"><span class="function">                                                    <span class="type">mca_coll_base_module_t</span> *<span class="keyword">module</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> size = <span class="built_in">ompi_comm_size</span>(comm);</span><br><span class="line">    <span class="type">int</span> rank = <span class="built_in">ompi_comm_rank</span>(comm);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span> (<span class="number">1</span> == size) &#123;</span><br><span class="line">        <span class="keyword">if</span> (MPI_IN_PLACE != sbuf) <span class="comment">//copy data form sbuf to rbuf;</span></span><br><span class="line">        <span class="keyword">return</span> MPI_SUCCESS;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Determine nearest power of two less than or equal to size */</span></span><br><span class="line">    adjsize = <span class="built_in">opal_next_poweroftwo</span> (size);</span><br><span class="line">    adjsize &gt;&gt;= <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Handle non-power-of-two case:</span></span><br><span class="line"><span class="comment">       - Even ranks less than 2 * extra_ranks send their data to (rank + 1), and sets new rank to -1.</span></span><br><span class="line"><span class="comment">       - Odd ranks less than 2 * extra_ranks receive data from (rank - 1), apply appropriate operation, and set new rank to rank/2</span></span><br><span class="line"><span class="comment">       - Everyone else sets rank to rank - extra_ranks</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line"></span><br><span class="line">    extra_ranks = size - adjsize;</span><br><span class="line">    <span class="keyword">if</span> (rank &lt;  (<span class="number">2</span> * extra_ranks)) </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="number">0</span> == (rank % <span class="number">2</span>)) &#123;</span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">send</span>(tmpsend, count, dtype, (rank + <span class="number">1</span>), ..., comm));</span><br><span class="line">            newrank = <span class="number">-1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">recv</span>(tmprecv, count, dtype, (rank - <span class="number">1</span>), ..., comm, ...));</span><br><span class="line">            <span class="comment">/* tmpsend = tmprecv (op) tmpsend */</span></span><br><span class="line">            <span class="built_in">ompi_op_reduce</span>(op, tmprecv, tmpsend, count, dtype);</span><br><span class="line">            newrank = rank &gt;&gt; <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> &#123;newrank = rank - extra_ranks;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Communication/Computation loop</span></span><br><span class="line"><span class="comment">       - Exchange message with remote node.</span></span><br><span class="line"><span class="comment">       - Perform appropriate operation taking in account order of operations:</span></span><br><span class="line"><span class="comment">       result = value (op) result</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="keyword">for</span> (distance = <span class="number">0x1</span>; distance &lt; adjsize; distance &lt;&lt;=<span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (newrank &lt; <span class="number">0</span>) <span class="keyword">break</span>;</span><br><span class="line">        <span class="comment">/* Determine remote node (异或操作)*/</span></span><br><span class="line">        newremote = newrank ^ distance;</span><br><span class="line">        remote = (newremote &lt; extra_ranks)? (newremote * <span class="number">2</span> + <span class="number">1</span>):(newremote + extra_ranks);</span><br><span class="line"></span><br><span class="line">        <span class="comment">/* Exchange the data */</span></span><br><span class="line">        ret = <span class="built_in">ompi_coll_base_sendrecv_actual</span>(tmpsend, count, dtype, remote, ..., tmprecv, count, dtype, remote, ..., comm, ...);</span><br><span class="line"></span><br><span class="line">        <span class="comment">/* Apply operation */</span></span><br><span class="line">        <span class="keyword">if</span> (rank &lt; remote) &#123;</span><br><span class="line">            <span class="comment">/* tmprecv = tmpsend (op) tmprecv, ompi_op_reduce(op, soruce, target, ...)*/</span></span><br><span class="line">            <span class="built_in">ompi_op_reduce</span>(op, tmpsend, tmprecv, count, dtype);</span><br><span class="line">            tmpswap = tmprecv;</span><br><span class="line">            tmprecv = tmpsend;</span><br><span class="line">            tmpsend = tmpswap;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">/* tmpsend = tmprecv (op) tmpsend */</span></span><br><span class="line">            <span class="built_in">ompi_op_reduce</span>(op, tmprecv, tmpsend, count, dtype);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Handle non-power-of-two case:</span></span><br><span class="line"><span class="comment">       - Odd ranks less than 2 * extra_ranks send result from tmpsend to (rank - 1)</span></span><br><span class="line"><span class="comment">       - Even ranks less than 2 * extra_ranks receive result from (rank + 1)</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="keyword">if</span> (rank &lt; (<span class="number">2</span> * extra_ranks)) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="number">0</span> == (rank % <span class="number">2</span>)) &#123;</span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">recv</span>(rbuf, count, dtype, (rank + <span class="number">1</span>), ...));</span><br><span class="line">            tmpsend = (<span class="type">char</span>*)rbuf;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">send</span>(tmpsend, count, dtype, (rank - <span class="number">1</span>), ...));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Ensure that the final result is in rbuf */</span></span><br><span class="line">    <span class="keyword">if</span> (tmpsend != rbuf) <span class="comment">//copy form tmpsend to rbuf;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> MPI_SUCCESS;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注意新版$OpenMPI$中的$\verb+recursive doubling+$函数可以支持不满足交换律的操作，但是必须满足结合律。</p>
<h3 id="MPICH版-1"><a href="#MPICH版-1" class="headerlink" title="MPICH版"></a>MPICH版</h3><p>实现几乎完全一样，故略。</p>
<h2 id="4、reduce-scatter算法"><a href="#4、reduce-scatter算法" class="headerlink" title="4、reduce-scatter算法"></a>4、reduce-scatter算法</h2><h3 id="OpenMPI版-2"><a href="#OpenMPI版-2" class="headerlink" title="OpenMPI版"></a>OpenMPI版</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * This algorithm is a combination of a reduce-scatter implemented with</span></span><br><span class="line"><span class="comment"> * recursive vector halving and recursive distance doubling, followed either</span></span><br><span class="line"><span class="comment"> * by an allgather implemented with recursive doubling.</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * Limitations:</span></span><br><span class="line"><span class="comment"> *   count &gt;= 2^&#123;\floor&#123;\log_2 p&#125;&#125;</span></span><br><span class="line"><span class="comment"> *   commutative operations only</span></span><br><span class="line"><span class="comment"> *   intra-communicators only</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">ompi_coll_base_allreduce_intra_redscat_allgather</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *sbuf, </span></span></span><br><span class="line"><span class="params"><span class="function">    <span class="type">void</span> *rbuf, <span class="type">int</span> count, <span class="keyword">struct</span> <span class="type">ompi_datatype_t</span> *dtype,</span></span></span><br><span class="line"><span class="params"><span class="function">    <span class="keyword">struct</span> <span class="type">ompi_op_t</span> *op, <span class="keyword">struct</span> <span class="type">ompi_communicator_t</span> *comm,</span></span></span><br><span class="line"><span class="params"><span class="function">    <span class="type">mca_coll_base_module_t</span> *<span class="keyword">module</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> comm_size = <span class="built_in">ompi_comm_size</span>(comm);</span><br><span class="line">    <span class="type">int</span> rank = <span class="built_in">ompi_comm_rank</span>(comm);</span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> nsteps = <span class="built_in">opal_hibit</span>(comm_size, comm-&gt;c_cube_dim + <span class="number">1</span>);<span class="comment">/*ilog2(comm_size)*/</span></span><br><span class="line">    <span class="built_in">assert</span>(nsteps &gt;= <span class="number">0</span>);</span><br><span class="line">    <span class="type">int</span> nprocs_pof2 = <span class="number">1</span> &lt;&lt; nsteps;                   <span class="comment">/* flp2(comm_size) */</span></span><br><span class="line"></span><br><span class="line">    <span class="type">ptrdiff_t</span> lb, extent, dsize, gap = <span class="number">0</span>;</span><br><span class="line">    <span class="built_in">ompi_datatype_get_extent</span>(dtype, &amp;lb, &amp;extent);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (count &lt; nprocs_pof2 || !<span class="built_in">ompi_op_is_commute</span>(op)) </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">ompi_coll_base_allreduce_intra_basic_linear</span>(sbuf, rbuf, count, dtype, op, comm, <span class="keyword">module</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (sbuf != MPI_IN_PLACE) <span class="comment">//copy from sbuf to rbuf;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * Step 1. Reduce the number of processes to the nearest lower power of two</span></span><br><span class="line"><span class="comment">     * p&#x27; = 2^&#123;\floor&#123;\log_2 p&#125;&#125; by removing r = p - p&#x27; processes.</span></span><br><span class="line"><span class="comment">     * 1. All the even ranks send the second half of  the input vector to their right neighbor (rank + 1),</span></span><br><span class="line"><span class="comment">     *    and all the odd ranks send the first half of the input vector to their left neighbor (rank - 1).</span></span><br><span class="line"><span class="comment">     * 2. All 2r processes compute the reduction on their half.</span></span><br><span class="line"><span class="comment">     * 3. The odd ranks then send the result to their left neighbors (the even ranks).</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="type">int</span> vrank, step, wsize;</span><br><span class="line">    <span class="type">int</span> nprocs_rem = comm_size - nprocs_pof2;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (rank &lt; <span class="number">2</span> * nprocs_rem)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="type">int</span> count_lhalf = count / <span class="number">2</span>;</span><br><span class="line">        <span class="type">int</span> count_rhalf = count - count_lhalf;</span><br><span class="line">        <span class="keyword">if</span> (rank % <span class="number">2</span> != <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">/*</span></span><br><span class="line"><span class="comment">             * Odd process -- exchange with (rank - 1)</span></span><br><span class="line"><span class="comment">             * Send the left half of the input vector to the left neighbor,</span></span><br><span class="line"><span class="comment">             * Recv the right half of the input vector from the left neighbor</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            <span class="built_in">ompi_coll_base_sendrecv</span>(rbuf, count_lhalf, dtype, rank - <span class="number">1</span>, ...,</span><br><span class="line">                            (<span class="type">char</span> *)tmp_buf + (<span class="type">ptrdiff_t</span>)count_lhalf * extent,</span><br><span class="line">                            count_rhalf, dtype, rank - <span class="number">1</span>, ..., rank);</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* Reduce on the right half of the buffers (result in rbuf) */</span></span><br><span class="line">            <span class="built_in">ompi_op_reduce</span>(op, (<span class="type">char</span> *)tmp_buf + (<span class="type">ptrdiff_t</span>)count_lhalf * extent,</span><br><span class="line">                        (<span class="type">char</span> *)rbuf + count_lhalf * extent, count_rhalf, dtype);</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* Send the right half to the left neighbor */</span></span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">send</span>((<span class="type">char</span> *)rbuf + (<span class="type">ptrdiff_t</span>)count_lhalf * extent,</span><br><span class="line">                               count_rhalf, dtype, rank - <span class="number">1</span>, ..., comm));</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* This process does not pariticipate in recursive doubling phase */</span></span><br><span class="line">            vrank = <span class="number">-1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">/*</span></span><br><span class="line"><span class="comment">             * Even process -- exchange with rank + 1</span></span><br><span class="line"><span class="comment">             * Send the right half of the input vector to the right neighbor,</span></span><br><span class="line"><span class="comment">             * Recv the left half of the input vector from the right neighbor</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            <span class="built_in">ompi_coll_base_sendrecv</span>((<span class="type">char</span> *)rbuf + (<span class="type">ptrdiff_t</span>)count_lhalf*extent,</span><br><span class="line">                            count_rhalf, dtype, rank + <span class="number">1</span>, ...,tmp_buf, </span><br><span class="line">                            count_lhalf, dtype, rank + <span class="number">1</span>, ..., comm, ..., rank);</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* Reduce on the left half of the buffers (result in rbuf) */</span></span><br><span class="line">            <span class="built_in">ompi_op_reduce</span>(op, tmp_buf, rbuf, count_lhalf, dtype);</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* Recv the right half from the right neighbor */</span></span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">recv</span>((<span class="type">char</span> *)rbuf + (<span class="type">ptrdiff_t</span>)count_lhalf * extent,</span><br><span class="line">                            count_rhalf, dtype, rank + <span class="number">1</span>, ..., comm, ...));</span><br><span class="line"></span><br><span class="line">            vrank = rank / <span class="number">2</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> </span><br><span class="line">    &#123; <span class="comment">/* rank &gt;= 2 * nprocs_rem */</span></span><br><span class="line">        vrank = rank - nprocs_rem;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * Step 2. Reduce-scatter implemented with recursive vector halving and</span></span><br><span class="line"><span class="comment">     * recursive distance doubling. We have  new ranks and result in rbuf.</span></span><br><span class="line"><span class="comment">     *At the end, each process has 1 / p&#x27; of the total reduction result.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="type">int</span>* rindex = <span class="built_in">malloc</span>(<span class="built_in">sizeof</span>(*rindex) * nsteps);</span><br><span class="line">    <span class="type">int</span>* sindex = <span class="built_in">malloc</span>(<span class="built_in">sizeof</span>(*sindex) * nsteps);</span><br><span class="line">    <span class="type">int</span>* rcount = <span class="built_in">malloc</span>(<span class="built_in">sizeof</span>(*rcount) * nsteps);</span><br><span class="line">    <span class="type">int</span>* scount = <span class="built_in">malloc</span>(<span class="built_in">sizeof</span>(*scount) * nsteps);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (vrank != <span class="number">-1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        step = <span class="number">0</span>;</span><br><span class="line">        wsize = count;</span><br><span class="line">        sindex[<span class="number">0</span>] = rindex[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> mask = <span class="number">1</span>; mask &lt; nprocs_pof2; mask &lt;&lt;= <span class="number">1</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">/* On each iteration: rindex[step] = sindex[step] -- begining of the</span></span><br><span class="line"><span class="comment">            current window. Length of the current window is stored in wsize.*/</span></span><br><span class="line">            <span class="type">int</span> vdest = vrank ^ mask;</span><br><span class="line">            <span class="comment">/* Translate vdest virtual rank to real rank */</span></span><br><span class="line">            <span class="type">int</span> dest = (vdest &lt; nprocs_rem) ? vdest * <span class="number">2</span> : vdest + nprocs_rem;</span><br><span class="line">            <span class="keyword">if</span> (rank &lt; dest) &#123;</span><br><span class="line">                <span class="comment">/*</span></span><br><span class="line"><span class="comment">                 * Recv into the left half of the current window, send the right</span></span><br><span class="line"><span class="comment">                 * half of the window to the peer (perform reduce on the left</span></span><br><span class="line"><span class="comment">                 * half of the current window)</span></span><br><span class="line"><span class="comment">                 */</span></span><br><span class="line">                rcount[step] = wsize / <span class="number">2</span>;</span><br><span class="line">                scount[step] = wsize - rcount[step];</span><br><span class="line">                sindex[step] = rindex[step] + rcount[step];</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">/*</span></span><br><span class="line"><span class="comment">                 * Recv into the right half of the current window, send the left</span></span><br><span class="line"><span class="comment">                 * half of the window to the peer (perform reduce on the right</span></span><br><span class="line"><span class="comment">                 * half of the current window)</span></span><br><span class="line"><span class="comment">                 */</span></span><br><span class="line">                scount[step] = wsize / <span class="number">2</span>;</span><br><span class="line">                rcount[step] = wsize - scount[step];</span><br><span class="line">                rindex[step] = sindex[step] + scount[step];</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* Send part of data from the rbuf, recv into the tmp_buf */</span></span><br><span class="line">            <span class="built_in">ompi_coll_base_sendrecv</span>((<span class="type">char</span> *)rbuf + (<span class="type">ptrdiff_t</span>)sindex[step] * extent,</span><br><span class="line">                            scount[step], dtype, dest, ...,</span><br><span class="line">                            (<span class="type">char</span> *)tmp_buf + (<span class="type">ptrdiff_t</span>)rindex[step] * extent, </span><br><span class="line">                            rcount[step], dtype, dest, ..., comm, ..., rank);</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* Local reduce: rbuf[] = tmp_buf[] &lt;op&gt; rbuf[] */</span></span><br><span class="line">            <span class="built_in">ompi_op_reduce</span>(op, (<span class="type">char</span>*)tmp_buf + (<span class="type">ptrdiff_t</span>)rindex[step] * extent,</span><br><span class="line">                           (<span class="type">char</span> *)rbuf + (<span class="type">ptrdiff_t</span>)rindex[step] * extent,</span><br><span class="line">                           rcount[step], dtype);</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* Move the current window to the received message */</span></span><br><span class="line">            <span class="keyword">if</span> (step + <span class="number">1</span> &lt; nsteps) &#123;</span><br><span class="line">                rindex[step + <span class="number">1</span>] = rindex[step];</span><br><span class="line">                sindex[step + <span class="number">1</span>] = rindex[step];</span><br><span class="line">                wsize = rcount[step];</span><br><span class="line">                step++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        * Step 3. Allgather by the recursive doubling algorithm.</span></span><br><span class="line"><span class="comment">        * Each process has 1 / p&#x27; of the total reduction result:</span></span><br><span class="line"><span class="comment">        * rcount[nsteps - 1] elements in the rbuf[rindex[nsteps - 1], ...].</span></span><br><span class="line"><span class="comment">        * All exchanges are executed in reverse order relative to recursive doubling (previous step).</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">        step = nsteps - <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> mask = nprocs_pof2 &gt;&gt; <span class="number">1</span>; mask &gt; <span class="number">0</span>; mask &gt;&gt;= <span class="number">1</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="type">int</span> vdest = vrank ^ mask;</span><br><span class="line">            <span class="comment">/* Translate vdest virtual rank to real rank */</span></span><br><span class="line">            <span class="type">int</span> dest = (vdest &lt; nprocs_rem) ? vdest * <span class="number">2</span> : vdest + nprocs_rem;</span><br><span class="line">            <span class="comment">/* Send rcount[step] elements from rbuf[rindex[step]...]</span></span><br><span class="line"><span class="comment">               Recv scount[step] elements to rbuf[sindex[step]...] */</span></span><br><span class="line">            <span class="built_in">ompi_coll_base_sendrecv</span>((<span class="type">char</span> *)rbuf + (<span class="type">ptrdiff_t</span>)rindex[step] * extent,</span><br><span class="line">                                rcount[step], dtype, dest, ...,</span><br><span class="line">                                (<span class="type">char</span> *)rbuf + (<span class="type">ptrdiff_t</span>)sindex[step] * extent,</span><br><span class="line">                                scount[step], dtype, dest, ..., comm, ..., rank);</span><br><span class="line">            step--;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Step 4. Send total result to excluded odd ranks. */</span></span><br><span class="line">    <span class="keyword">if</span> (rank &lt; <span class="number">2</span> * nprocs_rem)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span>(rank % <span class="number">2</span> != <span class="number">0</span>) <span class="comment">/* Odd process -- recv result from rank - 1 */</span></span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">recv</span>(rbuf, count, dtype, rank - <span class="number">1</span>, ..., comm, ...));</span><br><span class="line">        <span class="keyword">else</span> <span class="comment">/* Even process -- send result to rank + 1 */</span></span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">send</span>(rbuf, count, dtype, rank + <span class="number">1</span>, ..., comm));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//free buffer rindex, sindex, rcount, scount;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="5、ring算法"><a href="#5、ring算法" class="headerlink" title="5、ring算法"></a>5、ring算法</h2><h3 id="OpenMPI版-3"><a href="#OpenMPI版-3" class="headerlink" title="OpenMPI版"></a>OpenMPI版</h3><p>注：只有在$OpenMPI$中才有$Ring$ $Allreduce$。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 消息自动被分成M/N段，算法执行2*N - 1步。算法不能保证规约操作顺序，所以仅支持满足交换律的操作，并且消息量小于进程数时算法不能正常工作。</span></span><br><span class="line"><span class="comment"> * Example on 5 nodes:</span></span><br><span class="line"><span class="comment"> *         Initial state</span></span><br><span class="line"><span class="comment"> *   #      0              1             2              3             4</span></span><br><span class="line"><span class="comment"> *        [00]           [10]          [20]           [30]           [40]</span></span><br><span class="line"><span class="comment"> *        [01]           [11]          [21]           [31]           [41]</span></span><br><span class="line"><span class="comment"> *        [02]           [12]          [22]           [32]           [42]</span></span><br><span class="line"><span class="comment"> *        [03]           [13]          [23]           [33]           [43]</span></span><br><span class="line"><span class="comment"> *        [04]           [14]          [24]           [34]           [44]</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  计算阶段</span></span><br><span class="line"><span class="comment"> *  Step 0: rank r sends block r to rank (r+1) and receives bloc (r-1)</span></span><br><span class="line"><span class="comment"> *  from rank (r-1) [with wraparound].</span></span><br><span class="line"><span class="comment"> *    #     0              1             2              3             4</span></span><br><span class="line"><span class="comment"> *        [00]          [00+10]        [20]           [30]           [40]</span></span><br><span class="line"><span class="comment"> *        [01]           [11]         [11+21]         [31]           [41]</span></span><br><span class="line"><span class="comment"> *        [02]           [12]          [22]          [22+32]         [42]</span></span><br><span class="line"><span class="comment"> *        [03]           [13]          [23]           [33]         [33+43]</span></span><br><span class="line"><span class="comment"> *      [44+04]          [14]          [24]           [34]           [44]</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  Step 1: rank r sends block (r-1) to rank (r+1) and receives bloc</span></span><br><span class="line"><span class="comment"> *  (r-2) from rank (r-1) [with wraparound].</span></span><br><span class="line"><span class="comment"> *    #      0              1             2              3             4</span></span><br><span class="line"><span class="comment"> *         [00]          [00+10]     [01+10+20]        [30]           [40]</span></span><br><span class="line"><span class="comment"> *         [01]           [11]         [11+21]      [11+21+31]        [41]</span></span><br><span class="line"><span class="comment"> *         [02]           [12]          [22]          [22+32]      [22+32+42]</span></span><br><span class="line"><span class="comment"> *      [33+43+03]        [13]          [23]           [33]         [33+43]</span></span><br><span class="line"><span class="comment"> *        [44+04]       [44+04+14]       [24]           [34]           [44]</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  Step 2: rank r sends block (r-2) to rank (r+1) and receives bloc</span></span><br><span class="line"><span class="comment"> *  (r-2) from rank (r-1) [with wraparound].</span></span><br><span class="line"><span class="comment"> *    #      0              1             2              3             4</span></span><br><span class="line"><span class="comment"> *         [00]          [00+10]     [01+10+20]    [01+10+20+30]      [40]</span></span><br><span class="line"><span class="comment"> *         [01]           [11]         [11+21]      [11+21+31]    [11+21+31+41]</span></span><br><span class="line"><span class="comment"> *     [22+32+42+02]      [12]          [22]          [22+32]      [22+32+42]</span></span><br><span class="line"><span class="comment"> *      [33+43+03]    [33+43+03+13]     [23]           [33]         [33+43]</span></span><br><span class="line"><span class="comment"> *        [44+04]       [44+04+14]  [44+04+14+24]      [34]           [44]</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  Step 3: rank r sends block (r-3) to rank (r+1) and receives bloc</span></span><br><span class="line"><span class="comment"> *   (r-3) from rank (r-1) [with wraparound].</span></span><br><span class="line"><span class="comment"> *    #      0              1             2              3             4</span></span><br><span class="line"><span class="comment"> *         [00]          [00+10]     [01+10+20]    [01+10+20+30]     [FULL]</span></span><br><span class="line"><span class="comment"> *        [FULL]           [11]        [11+21]      [11+21+31]    [11+21+31+41]</span></span><br><span class="line"><span class="comment"> *     [22+32+42+02]     [FULL]          [22]         [22+32]      [22+32+42]</span></span><br><span class="line"><span class="comment"> *      [33+43+03]    [33+43+03+13]     [FULL]          [33]         [33+43]</span></span><br><span class="line"><span class="comment"> *        [44+04]       [44+04+14]  [44+04+14+24]      [FULL]         [44]</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  分布阶段：ring ALLGATHER with ranks shifted by 1.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">ompi_coll_base_allreduce_intra_ring</span><span class="params">(...)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> size = <span class="built_in">ompi_comm_size</span>(comm);</span><br><span class="line">    <span class="type">int</span> rank = <span class="built_in">ompi_comm_rank</span>(comm);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (<span class="number">1</span> == size) <span class="comment">//copy from sbuf to rbuf</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//若消息量小于进程数就使用recursive doubling</span></span><br><span class="line">    <span class="keyword">if</span> (count &lt; size) </span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">ompi_coll_base_allreduce_intra_recursivedoubling</span>(sbuf, rbuf, count, dtype, op, comm, <span class="keyword">module</span>);</span><br><span class="line"></span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="https://chudod.gitee.io/myworks/2022/04/13/OpenMPI&MPICH_Allreduce/" data-id="cl3kznr07000vj0ukcwuf5emg" data-title="MPI_Allreduce的在OpenMPI、MPICH中的实现" class="article-share-link">Share</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/myworks/tags/MPI/" rel="tag">MPI</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/myworks/tags/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/" rel="tag">并行计算</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/myworks/2022/04/14/OS_Memory_Management/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          操作系统内存管理
        
      </div>
    </a>
  
  
    <a href="/myworks/2022/04/10/Guiding_Board/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Read Me（导航栏）</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/myworks/categories/English/">English</a></li><li class="category-list-item"><a class="category-list-link" href="/myworks/categories/%D0%A0%D0%BE%D1%81%D1%81%D0%B8%D1%8F/">Россия</a></li><li class="category-list-item"><a class="category-list-link" href="/myworks/categories/%E5%AF%BC%E8%88%AA/">导航</a></li><li class="category-list-item"><a class="category-list-link" href="/myworks/categories/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/">并行计算</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/myworks/categories/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/In-Network-Collectives/">In-Network Collectives</a></li><li class="category-list-item"><a class="category-list-link" href="/myworks/categories/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/MPI%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1/">MPI程序设计</a></li><li class="category-list-item"><a class="category-list-link" href="/myworks/categories/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/MPI%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E4%B8%8E%E5%B9%B6%E8%A1%8C%E7%AE%97%E6%B3%95/">MPI程序设计与并行算法</a></li><li class="category-list-item"><a class="category-list-link" href="/myworks/categories/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/MPI%E8%BD%AF%E4%BB%B6%E6%A0%88%E8%AE%BE%E8%AE%A1/">MPI软件栈设计</a></li><li class="category-list-item"><a class="category-list-link" href="/myworks/categories/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/MPI%E9%9B%86%E5%90%88%E9%80%9A%E4%BF%A1%E7%AE%97%E6%B3%95/">MPI集合通信算法</a></li><li class="category-list-item"><a class="category-list-link" href="/myworks/categories/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/Topology-Aware-Algorithms/">Topology-Aware Algorithms</a></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/myworks/categories/%E6%95%B4%E6%B4%BB%E7%B3%BB%E5%88%97/">整活系列</a></li><li class="category-list-item"><a class="category-list-link" href="/myworks/categories/%E7%A1%AC%E6%A0%B8%E7%8B%A0%E4%BA%BA/">硬核狠人</a></li><li class="category-list-item"><a class="category-list-link" href="/myworks/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">计算机基础知识</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/myworks/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">操作系统</a></li></ul></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/myworks/tags/English-Vocabulary/" rel="tag">English Vocabulary</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myworks/tags/English-Writing/" rel="tag">English Writing</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myworks/tags/MPI/" rel="tag">MPI</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myworks/tags/Topology-Aware/" rel="tag">Topology-Aware</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myworks/tags/%D0%A0%D0%BE%D1%81%D1%81%D0%B8%D1%8F/" rel="tag">Россия</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myworks/tags/%E5%9C%A8%E7%BD%91%E8%AE%A1%E7%AE%97/" rel="tag">在网计算</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myworks/tags/%E5%B9%B6%E8%A1%8C%E7%AE%97%E6%B3%95/" rel="tag">并行算法</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myworks/tags/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/" rel="tag">并行计算</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myworks/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="tag">操作系统</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myworks/tags/%E6%95%B4%E6%B4%BB/" rel="tag">整活</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myworks/tags/%E7%94%9F%E6%B4%BB/" rel="tag">生活</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myworks/tags/%E7%A1%AC%E6%A0%B8%E7%8B%A0%E4%BA%BA/" rel="tag">硬核狠人</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myworks/tags/%E9%9B%86%E5%90%88%E9%80%9A%E4%BF%A1/" rel="tag">集合通信</a></li><li class="tag-list-item"><a class="tag-list-link" href="/myworks/tags/%E9%AB%98%E6%80%A7%E8%83%BD%E8%AE%A1%E7%AE%97/" rel="tag">高性能计算</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/myworks/tags/English-Vocabulary/" style="font-size: 10px;">English Vocabulary</a> <a href="/myworks/tags/English-Writing/" style="font-size: 10px;">English Writing</a> <a href="/myworks/tags/MPI/" style="font-size: 17.5px;">MPI</a> <a href="/myworks/tags/Topology-Aware/" style="font-size: 10px;">Topology-Aware</a> <a href="/myworks/tags/%D0%A0%D0%BE%D1%81%D1%81%D0%B8%D1%8F/" style="font-size: 12.5px;">Россия</a> <a href="/myworks/tags/%E5%9C%A8%E7%BD%91%E8%AE%A1%E7%AE%97/" style="font-size: 12.5px;">在网计算</a> <a href="/myworks/tags/%E5%B9%B6%E8%A1%8C%E7%AE%97%E6%B3%95/" style="font-size: 10px;">并行算法</a> <a href="/myworks/tags/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/" style="font-size: 20px;">并行计算</a> <a href="/myworks/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" style="font-size: 15px;">操作系统</a> <a href="/myworks/tags/%E6%95%B4%E6%B4%BB/" style="font-size: 10px;">整活</a> <a href="/myworks/tags/%E7%94%9F%E6%B4%BB/" style="font-size: 10px;">生活</a> <a href="/myworks/tags/%E7%A1%AC%E6%A0%B8%E7%8B%A0%E4%BA%BA/" style="font-size: 10px;">硬核狠人</a> <a href="/myworks/tags/%E9%9B%86%E5%90%88%E9%80%9A%E4%BF%A1/" style="font-size: 15px;">集合通信</a> <a href="/myworks/tags/%E9%AB%98%E6%80%A7%E8%83%BD%E8%AE%A1%E7%AE%97/" style="font-size: 12.5px;">高性能计算</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/myworks/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myworks/archives/2022/04/">April 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/myworks/archives/2022/03/">March 2022</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/myworks/2022/05/24/Russian_Grammar/">Русская Граммаатика(俄语语法)</a>
          </li>
        
          <li>
            <a href="/myworks/2022/05/18/OS_FileSystem/">文件系统概念和Ext2文件系统</a>
          </li>
        
          <li>
            <a href="/myworks/2022/05/14/English_Sentences/">English Sentences</a>
          </li>
        
          <li>
            <a href="/myworks/2022/05/11/English_Vocabulary/">English Vocabulary</a>
          </li>
        
          <li>
            <a href="/myworks/2022/05/11/Russian_Vocabulary/">Русские Слова(俄语单词)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2022 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/myworks/" class="mobile-nav-link">Home</a>
  
    <a href="/myworks/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/myworks/js/jquery-3.4.1.min.js"></script>



  
<script src="/myworks/fancybox/jquery.fancybox.min.js"></script>




<script src="/myworks/js/script.js"></script>





  </div>
</body>
</html>